package cn.threecubic.sort;

import java.util.Arrays;

/**
 * 希尔排序
 *
 * @author Terminator
 * @date 2021/10/19 9:09
 */
public class ShellSort {

    public static void main(String[] args) {
        int[] data = {35, 33, 42, 10, 14, 19, 27, 44};
        System.out.println("希尔排序,排序前：" + Arrays.toString(data));
        shellSort(data);
        System.out.println("希尔排序,排序后：" + Arrays.toString(data));
    }

    /**
     * 希尔排序
     *
     * @param arr
     */
    public static void shellSort(int[] arr) {
        int length = arr.length;
        for (int step = length / 2; step >= 1; step /= 2) {
            for (int i = step; i < length; i++) {
                int dataTemp = arr[i];
                int indexTemp = i - step;
                while (indexTemp >= 0 && arr[indexTemp] > dataTemp) {
                    arr[indexTemp + step] = arr[indexTemp];
                    indexTemp -= step;
                }
                arr[indexTemp + step] = dataTemp;
            }
        }
    }

}
